﻿using System;
using System.Threading;
using System.Windows.Forms;
using DCS.AdministrationClient.DCS_Service;
using DCS.AdministrationClient.Properties;
using DCS.Extensions.WindowsForms;

namespace DCS.AdministrationClient.Forms
{
    public partial class PreferencesForm : Form
    {
        public PreferencesForm()
        {
            InitializeComponent();
        }

        private void PreferencesFormLoad(object sender, EventArgs e)
        {
            var centralServer = Program.Context.DcsService.GetCentralServerAddress();
            centralServerAddress.SafeTextSet(centralServer);
            serverType.SelectedIndex = string.IsNullOrEmpty(centralServer) ? 0 : 1;

            header.SafeTextSet(string.Format("{0} {1}", header.SafeTextGet(), Program.Context.Url));
        }

        private void SubmitClick(object sender, EventArgs e)
        {
            Program.Context.DcsService.SetCentralServerAddress(centralServerAddress.Text);
        }

        private void VerifyServerClick(object sender, EventArgs e)
        {
            new Thread(VerifyAddress).Start();
        }

        private void VerifyAddress()
        {
            try
            {
                StartVerification();

                var server = new DcsService(string.Format("http://{0}/DcsService.asmx",
                                             centralServerAddress.SafeTextGet()));
                server.StartAdminSession();

                FinishVerification(true);
            }
            catch
            {
                FinishVerification(false);
            }
        }

        private void StartVerification()
        {
            verificationResultPicture.SafePictureBoxSet(Resources.load);
            verifyServer.SafeDisable();
        }

        private void FinishVerification(bool result)
        {
            if (result)
            {
                verificationResultPicture.SafePictureBoxSet(Resources.connectionOk);
                centralServerAddressLabel.SafeTextSet("Сервер доступен");
            }
            else
            {
                verificationResultPicture.SafePictureBoxSet(Resources.connectionFail);
                centralServerAddressLabel.SafeTextSet("Сервер не обнаружен");
            }
            verifyServer.SafeEnable();
        }

        private void PreferencesFormClosing(object sender, FormClosingEventArgs e)
        {
            Program.Greetings.Show();
        }

        private void ResetButtonClick(object sender, EventArgs e)
        {
            var dialogResult = MessageBox.Show(Resources.PreferencesForm_ResetButtonClick_,
                                               Resources.ResetButtonClickAttentionMessage,
                                               MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                               MessageBoxDefaultButton.Button2);
            if (dialogResult == DialogResult.Yes)
            {
                Program.Context.DcsService.RestartService();
            }
        }

        private void ExitClick(object sender, EventArgs e)
        {
            this.SafeClose();
        }

        private void ServerTypeSelectedIndexChanged(object sender, EventArgs e)
        {
            var isCentralServer = serverType.SelectedIndex == 1;
            
            centralServerAddressLabel.Enabled = isCentralServer;
            centralServerAddress.Enabled = isCentralServer;
            verifyServer.Enabled = isCentralServer;
            verificationResultPicture.Enabled = isCentralServer;

        }
    }
}